package com.lee.mao.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.lee.mao.R;
import com.lee.mao.bean.MoneyTable;
import com.lee.mao.database.utils.DateHelper;
import com.lee.mao.utils.AutoNextLineLinearLayout;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

public class ChargeAdapter extends RecyclerView.Adapter<ChargeAdapter.ViewHolder> {

    private List<List<MoneyTable>> moneyTableList;
    private Context context;

    public ChargeAdapter(Context context, List<List<MoneyTable>> moneyTableList) {
        this.context = context;
        this.moneyTableList = moneyTableList;
    }

    public void updateData(List<List<MoneyTable>> moneyTableList) {
        this.moneyTableList = moneyTableList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_charge, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if (moneyTableList == null || moneyTableList.size() < 1)
            return;

        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH);
        // 绑定数据
        if (moneyTableList.get(position) != null && moneyTableList.get(position).size() > 0 && moneyTableList.get(position).get(0) != null)
            holder.tvDate.setText(DateHelper.getTimeByFormat(DateHelper.FORMAT_YYM, moneyTableList.get(position).get(0).getChargeTime()));
        else {
            c.set(Calendar.MONTH, currentMonth - position);
            holder.tvDate.setText(DateHelper.getTimeByFormat(DateHelper.FORMAT_YYM, c.getTime().getTime()));
        }

        BigDecimal total = new BigDecimal("0");
        BigDecimal today = new BigDecimal("0");
        holder.autoLayout.removeAllViews();
        Calendar cDay = Calendar.getInstance();

        for (int i = 0; i < moneyTableList.get(position).size(); i++) {

            if (moneyTableList.get(position) == null)
                continue;

            List<MoneyTable> moneyTables = moneyTableList.get(position);

            if (moneyTables == null || moneyTables.size() < 1)
                continue;

            MoneyTable moneyTable = moneyTables.get(i);
            if (moneyTable == null)
                continue;

            TextView textView = new TextView(context);
            textView.setTextSize(14);

            long chargeTime = moneyTable.getChargeTime();
            Calendar c1 = Calendar.getInstance();
            c1.setTimeInMillis(chargeTime);

            if (c1.get(Calendar.MONTH) == cDay.get(Calendar.MONTH) &&
                    c1.get(Calendar.DAY_OF_MONTH) == cDay.get(Calendar.DAY_OF_MONTH) && c1.get(Calendar.YEAR) == cDay.get(Calendar.YEAR)) {
                today = total.add(new BigDecimal(moneyTable.getMoney()));
            }

            String str = "默认颜色<font color='#FF0000'><small>红颜色</small></font>";
            String money = moneyTable.getMoney();
            BigDecimal moneyDecimal = new BigDecimal(money);
            boolean b1 = moneyDecimal.intValue() < 100;
            String moneyStr = b1 ? money : "<font color='#ff3333'>" + money + "</font>";
            if (i == 0) {
                str = "<font color='#ffa31a'>" + DateHelper.getTimeByFormat(DateHelper.FORMAT_MD, moneyTable.getChargeTime()) + ":</font>" + moneyTable.getName() + " "
                        + moneyStr;
//                textView.setText(DateHelper.getTimeByFormat(DateHelper.FORMAT_MD, moneyTable.getChargeTime()) + ":" + moneyTable.getName() + " " + moneyTable.getMoney());
            } else {

                Calendar c2 = Calendar.getInstance();
                c2.setTimeInMillis(moneyTables.get(i - 1).getChargeTime());

                if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH) &&
                        c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH)) {
                    str = moneyTable.getName() + " "
                            + moneyStr;
                    //textView.setText(moneyTable.getName() + " " + moneyTable.getMoney());

                } else {
                    str = "<font color='#ffa31a'>" + DateHelper.getTimeByFormat(DateHelper.FORMAT_MD, moneyTable.getChargeTime()) + ":</font>" + moneyTable.getName() + " "
                            + moneyStr;
//                    textView.setText(DateHelper.getTimeByFormat(DateHelper.FORMAT_MD, moneyTable.getChargeTime()) + ":" + moneyTable.getName() + " " + moneyTable.getMoney());
                }

            }
            textView.setText(Html.fromHtml(str));
            //textView.setBackgroundColor(context.getResources().getColor(R.color.myGray));
            //textView.setBackgroundResource(R.drawable.selector_checkbox_bg_add_personal_info);
            AutoNextLineLinearLayout.LayoutParams params = new AutoNextLineLinearLayout
                    .LayoutParams(AutoNextLineLinearLayout.LayoutParams.WRAP_CONTENT,
                    AutoNextLineLinearLayout.LayoutParams.WRAP_CONTENT);
            //通过这个属性可以控制 排列方式
            params.setMargins(18, 18, 5, 5);
            textView.setLayoutParams(params);
            //通过这个属性可以控制 排列方式
            textView.setPadding(5, 5, 5, 5);
//            textView.setPadding(10, 10, 10, 10);
            //textView.setTextColor(context.getResources().getColor(R.color.white));
            //textView.setTextColor(getActivity().getResources().
            //        getColorStateList(R.drawable.selector_checkbox_text_color_add_personal_info));
            holder.autoLayout.addView(textView);

            //String money = moneyTableList.get(i).getMoney();
            BigDecimal b = new BigDecimal(moneyTableList.get(position).get(i).getMoney());
            total = total.add(b);
        }

        holder.total.setText("当天:" + today.toString() + "，总计:" + total.toString());

    }

    @Override
    public int getItemCount() {
        return moneyTableList == null ? 0 : moneyTableList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvDate;
        TextView total;
        AutoNextLineLinearLayout autoLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tvDate);
            total = itemView.findViewById(R.id.total);
            autoLayout = itemView.findViewById(R.id.autoLayout);
        }
    }

} 